![]() |
PATH![]() |
![]() ![]() |
This section describes several simple techniques you can use to help test and debug scripts. If you write large or complicated scripts, however, you should investigate a commercial script-debugging application from an independent software developer. For information on script debuggers, see the AppleScript website at
For debugging less complex scripts, try the following techniques:
say "Starting to empty the trash."
tell application "Finder"
empty trash
end tell
say "Finished emptying the trash."
if fileName is not equal to "Expected FileName" then
say "Unexpected file name."
end if
The Event Log helps you discover and correct errors by showing the results of a script's actions. You open the Script Editor's Event Log window from the Controls menu or by typing Command-E. The window contains two checkboxes. If you check Show Events, all Apple events are logged to the window. If you also check Show Event Results, the value returned from an Apple event is also displayed. (Results will not be returned unless both checkboxes are checked.)
In addition to simply opening the Event Log to view the results of actions taken by your script, you can insert log statements at strategic locations in your script. A log statement reports the value of one or more variables to the Event Log window.
Suppose you run the following script:
tell application "Finder"
set myFolder to first folder of startup disk
log (myFolder) --result: (*Claris Emailer Folder*)
end tell
With no checkboxes checked, the Event Log window contains just the result of the log (myFolder) statement:
(*Claris Emailer Folder*)
With just the Show Events checkbox checked, the Event Log window contains
tell application "Finder"
get folder 1 of startup disk
(*Claris Emailer Folder*)
end tell
Finally, with both checkboxes checked, the Event Log window contains
tell application "Finder"
get folder 1 of startup disk
--> folder "Claris Emailer Folder" of startup disk
(*Claris Emailer Folder*)
end tell
In this case, the log statement isn't really needed because it returns the same information displayed by checking the Show Event Results checkbox.
You can use start log and stop log statements to exert finer control over event logging. When the Show Events checkbox is checked, the "log level" count is set to 1. Whenever the log level is greater than 0, the Event Log window shows events. A start log statement increments the log level. A stop log statement decrements it. If the Show Events checkbox is checked, the following script turns off logging before getting the name of the first folder and turns it on again afterwards:
tell application "Finder"
stop log
set nameOne to name of first folder of startup disk
start log
set nameTwo to name of second folder of startup disk
end tell
Log statements can be especially useful when testing a Repeat loop or other control statement. In the following script, the statement log currentWord causes the current word to be displayed in the Script Editor's Event Log window each time through the loop. Once the loop is working correctly, you can comment out or delete the log statement.
set wordList to words in "Where is the hammer?"
--result: {"Where", "is", "the", "hammer"}
repeat with currentWord in wordList
log currentWord
if currentWord as text is equal to "hammer" then
display dialog "I found the hammer!"
end if
end repeat
This script examines a list of words with the Repeat With (loopVariable) In (list) form of the Repeat statement, displaying a dialog if it finds the word "hammer" in the list. For more information, see Repeat With (loopVariable) In (list).
For more information on the Script Editor's Event Log window, see the AppleScript section of the Mac OS Help Center.